//
//  StatusOriginalView.swift
//  weibo
//
//  Created by 谭仁川 on 15/12/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class StatusOriginalView: UIView {
    
    var bottomConstraints: Constraint?
    
    //添加模型属性
    var status: Status? {
        didSet {
            //绑定数据
            headImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrankImage
            verified_type_image.image = status?.user?.verified_type_image
            
            //TODO: 后期完善
            //            timeLabel.text = status?.created_at
            //            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            
            
            // 使用约束属性  记录可能产生复用的约束  将可能复用的约束 解除掉
            bottomConstraints?.uninstall()
            
            //设置配图视图的图片数组
            //智能提示不好使
            if let urls = status?.imageURLs where urls.count != 0 {
                //有配图视图
                //1.设置数据源
                pictureView.imageURLs = urls
                //2.更改视图的底部约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图视图  更改底部约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //记录底部约束
                    //1.对底部添加约束  2.转换成约束对象 并且使用属性记录
                    self.bottomConstraints =  make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
            }
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置ui
    private func setupUI() {
        
        sepView.backgroundColor = UIColor.lightGrayColor()
        addSubview(sepView)
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        //设置布局
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        headImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: StatusCellImageWidth, height: StatusCellImageWidth))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headImage.snp_top)
            
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_right)
            make.centerY.equalTo(headImage.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headImage.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(headImage.snp_bottom).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        
        //底部约束 由于需要根据具体的数据来进行更改 在setupUI中 无法拿到具体数据 无法在这个进行更改
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    
    private lazy var sepView: UIView = UIView()
    private lazy var headImage: UIImageView = UIImageView()
    private lazy var nameLabel: UILabel = UILabel(title: "我是川哥", color: UIColor.darkGrayColor(), fontSize: 14)
    private lazy var verified_type_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))

    private lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLabel: UILabel = UILabel(title: "小女孩", color: UIColor.darkGrayColor(), fontSize: 10)
    private lazy var contentLabel: UILabel = UILabel(title: "内心狂野的工程师", color: UIColor.darkGrayColor(), fontSize: 14,margin: StatusCellMargin)

    private lazy var pictureView: StatusPictureVIew = StatusPictureVIew()
}
